﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class DYCDingQiKongZhiXiangController : BaseController
    {
        private readonly ILogger<DYCDingQiKongZhiXiangController> _logger;
        public DYCDingQiKongZhiXiangController(ILogger<DYCDingQiKongZhiXiangController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index([FromForm] long dyclid, [FromForm] int pageIndex = 1, [FromForm] int pageSize = 10)
        {
            List<DYCDingQiKongZhiXiang> dycdingqikongzhixiangs = await _fsql.Select<DYCDingQiKongZhiXiang>().Where(dycdqkzx => dycdqkzx.Dyclid == dyclid).Count(out var total).Page(pageIndex, pageSize).ToListAsync();
            JObject data = new();
            data.Add("total", total);
            data.Add("list", JArray.FromObject(dycdingqikongzhixiangs));
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] DYCDingQiKongZhiXiang dycdingqikongzhixiang)
        {
            long id = await _fsql.Insert(dycdingqikongzhixiang).ExecuteIdentityAsync();
            dycdingqikongzhixiang.Id = id;

            return RenderJson(data: dycdingqikongzhixiang);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Delete([FromForm] long id)
        {
            int affrows = await _fsql.Delete<DYCDingQiKongZhiXiang>().Where(dycdqkzx => dycdqkzx.Id == id).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Update([FromForm] DYCDingQiKongZhiXiang dycdingqikongzhixiang)
        {
            int affrows = await _fsql.Update<DYCDingQiKongZhiXiang>().SetSourceIgnore(dycdingqikongzhixiang, colval => colval == null).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Show([FromForm] long id)
        {
            DYCDingQiKongZhiXiang dycdingqikongzhixiang = await _fsql.Select<DYCDingQiKongZhiXiang>().Where(dycdqkzx => dycdqkzx.Id == id).ToOneAsync();
            return RenderJson(data: dycdingqikongzhixiang);
        }
    }
}
